package com.aloda.ojquestion.pojo.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.aloda.ojcommon.domain.entity.JudgeConfig;
import com.aloda.ojcommon.domain.entity.Question;
import com.aloda.ojquestion.pojo.entity.QuestionPrompt;
import lombok.Data;

import java.util.List;

/**
 * 用户做题页面的题目详情信息
 *
 * @Author aloda
 * @Date 2025/4/28 18:06
 */
@Data
public class QuestionDetailVO {

    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目预设代码
     */
    private List<QuestionPrompt> prompt;

    /**
     * 是否通过该题目 1-通过 0-未通过
     */
    private Boolean passFlag;

    /**
     * 题目答案
     */
    private String answer;

    /**
     * 判题配置（json 对象）
     */
    private JudgeConfig judgeConfig;

    public static QuestionDetailVO of(Question question) {
        QuestionDetailVO questionDetailVO = BeanUtil.toBeanIgnoreError(question, QuestionDetailVO.class);
        questionDetailVO.setTags(JSONUtil.toList(question.getTags(), String.class));
        questionDetailVO.setJudgeConfig(JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class));
        questionDetailVO.setPrompt(JSONUtil.toList(question.getPrompt(), QuestionPrompt.class));
        return questionDetailVO;
    }

    public static QuestionDetailVO of(Question question, boolean isPass) {
        QuestionDetailVO questionDetailVO = of(question);
        questionDetailVO.setPassFlag(isPass);
        if (!isPass) {
            questionDetailVO.setAnswer("");
        }
        return questionDetailVO;
    }
}
